package com.qianyi.person.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.qianyi.person.annotations.ResponseResult;
import com.qianyi.person.annotations.SystemLog;
import com.qianyi.person.annotations.UserLoginToken;
import com.qianyi.person.domain.User;
import com.qianyi.person.service.TokenService;
import com.qianyi.person.service.UserService;

import com.qianyi.person.untils.DefaultErrorResult;
import com.qianyi.person.untils.PlatformResult;
import com.qianyi.person.untils.Result;
import com.qianyi.person.untils.ResultCode;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/v1")
@ResponseResult
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @SystemLog
    @Cacheable(value = "usercache",key = "'user_'+#id")
    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    //@Transactional(readOnly = true,value = "transactionManger")
   @UserLoginToken
   public Result selectByPrimaryKey(@PathVariable("id") Integer id){

       User user= userService.selectByPrimaryKey(id);
       if(user==null){
           return   DefaultErrorResult.failure(ResultCode.USER_NOT_EXIST,new NotFound(),HttpStatus.OK);
       }
       return   PlatformResult.success(user);
    }
    @Cacheable(value = "usercache",key = "'user_'+#offset +'_'+#limit")
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public PageInfo<User> selectAllUser(@RequestParam("offset") int offset, @RequestParam("limit") int limit){

        if (limit==0){
            limit=20;
        }
       return   userService.selectAllUser(offset,limit);
    }

    @CachePut(value = "usercache",key = "'user_'+#id")
    @RequestMapping(value = "/users/{id}",method = RequestMethod.PUT)
    public PageInfo<String>   updateByPrimaryKey(@PathVariable("id") Integer id){
        User user=userService.selectByPrimaryKey(id);
        if(user!=null){

        }
          return null;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(HttpServletRequest request, HttpServletResponse response) {
       String username=request.getParameter("username");
       String password=request.getParameter("password");
        JSONObject jsonObject=new JSONObject();
       User user=new User();
       user.setUsername(username);
       user.setPassword(password);
       User userForBase=userService.selectBynameAndpwd(user);
        if(userForBase==null){
            jsonObject.put("message","登录失败,用户不存在");
            return  DefaultErrorResult.failure(ResultCode.USER_NOT_EXIST,new NotFound(),HttpStatus.OK,jsonObject);
        } else {
            if (!userForBase.getPassword().equals(user.getPassword())){
                jsonObject.put("message","登录失败,密码错误");
                return DefaultErrorResult.failure(ResultCode.USER_NOT_EXIST,new NotFound(),HttpStatus.OK,jsonObject);
            }else {
                String token = tokenService.getToken(userForBase);
                jsonObject.put("token", token);
                jsonObject.put("user", userForBase);
                return PlatformResult.success(jsonObject);
            }
        }
    }


}
